package com.example.chatfree.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.avos.avoscloud.AVAnalytics;
import com.example.chatfree.R;

/**
 * 所有Activity的基类，里面放置公用方法和一些处理
 * Created by 周国谱 on 2015/10/8.
 */
public class BaseActivity extends AppCompatActivity {
    private Context mContext;
    private ProgressDialog mProgressDialog; //进度加载对话框
    private boolean mIsDestroy = false; //是否结束Activity
    private AlertDialog.Builder mBuilder;//显示提示的Dialog

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = BaseActivity.this;
        //LeanCloud 应用内统计分析
        AVAnalytics.trackAppOpened(getIntent());
    }

    protected void onDestroy() {
        super.onDestroy();
        mIsDestroy = true;
    }

    /**
     * 以对话框的形式显示提示信息
     *
     * @param errorMessage 要显示的提示信息
     * @param context      要显示的Activity
     */
    public void showDialogMessage(String errorMessage, Context context) {
        if (mBuilder == null) {
            mBuilder = new AlertDialog.Builder(context);
        }

        mBuilder.setTitle(
                context.getResources().getString(
                        R.string.tips))
                .setMessage(errorMessage)
                .setNegativeButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                dialog.dismiss();
                            }
                        }).show();
    }

    /**
     * 以对话框的形式显示提示信息
     *
     * @param message 要显示的提示信息
     * @param context 要显示的Activity
     */
    public void showToastMessage(String message, Context context) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示加载中对话框
     *
     * @param message 要显示的信息
     */
    public void showProgressDialog(CharSequence message) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setIndeterminate(true);
        }
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
    }

    /**
     * 隐藏加载对话框
     */
    public void dismissProgressDialog() {
        if (mProgressDialog != null && !mIsDestroy) {
            mProgressDialog.dismiss();
        }
    }
}
